package com.zhongjian.technologycal.mall.activity.personal.activity;


import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.WalletBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.PicassoRoundTransform;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.utils.ZXingUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.onekeyshare.OnekeyShare;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/9/25.
 * 我的二维码名片
 */

public class MineCodeCardActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.code_iv_heading)
    CircleImageView code_iv_heading;
    @BindView(R.id.code_tv_name)
    TextView code_tv_name;
    @BindView(R.id.code_tv_userid)
    TextView code_tv_userid;
    @BindView(R.id.code_iv_card)
    ImageView code_iv_card;
    private String iniviteCodeUrl;
    private Bitmap bitmap = null;
    private Dialog hintDialog = null;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_mine_code;
    }

    @Override
    protected void beforeInit() {
        super.beforeInit();


    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
//        StatusBarUtil.setColor(this, getResources().getColor(R.color.border_color), 0);
        tv_main_title.setText("我的名片");
        initViewRequest();
    }

    private void initViewRequest() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .getRequest(BaseContacts.URL_PERSONAL_WALLET + UserUtils.getUserToken(),
                        new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
//                                LoadingDialog.showDialogForLoading(MineMallWalletActivity.this);
                            }

                            @Override
                            public void onCompleted() {
//                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
//                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
//                                LoadingDialog.cancelDialogForLoading();
                                try {
                                    String result = responseBody.string();
                                    Gson gson = new Gson();
                                    WalletBean walletBean = gson.fromJson(result, WalletBean.class);
                                    if (walletBean.getError_code() == 0) {

                                        Picasso.get().load(BaseContacts.BASE_URL + walletBean.getData()
                                                .getPersonDataMap().getHeadPhoto())
                                                .transform(new PicassoRoundTransform())
                                                .into(code_iv_heading);

                                        code_tv_name.setText("" + walletBean.getData().getPersonDataMap().getUserName());
                                        code_tv_userid.setText("众健ID:" + walletBean.getData().getPersonDataMap().getSysID());

                                        String iniviteCode = String.valueOf(walletBean.getData().getPersonDataMap().getInviteCode());
                                        iniviteCodeUrl = BaseContacts.URL_MINE_CODE + iniviteCode;
                                        try {
                                            bitmap = ZXingUtils.createQRImage(BaseContacts.URL_MINE_CODE + iniviteCode);
//                                            bitmap = AppTools.create2DQrCode(BaseContacts.URL_MINE_CODE + iniviteCode);
//                                            Log.v("---", BaseContacts.URL_MINE_CODE + iniviteCode);
                                            code_iv_card.setImageBitmap(bitmap);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        } finally {

                                        }

                                    } else if (walletBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(walletBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });
    }

    @OnClick(R.id.code_tv_share)
    public void onClickShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setDialogMode(true);
        oks.setVenueDescription("分享");
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("我的名片(众健商城)");
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl(iniviteCodeUrl);
        // text是分享文本，所有平台都需要这个字段
//        oks.setText(iniviteCodeUrl);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        oks.setImageUrl("https://appnew.zhongjianmall.com/upload/pics/nohead.png");
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(iniviteCodeUrl);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
//        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(iniviteCodeUrl);
        // 启动分享GUI
        oks.show(this);

    }


    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!bitmap.isRecycled() && bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
    }
}
